# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def addTwoNumbers(self, l1, l2):
        l = ListNode(l1.val + l2.val)
        n1, n2, n = l1, l2, l
        c = 0
        while n1.next or n2.next:
            if n1.next != None and n2.next == None:
                n2.next = ListNode(0)
            elif n2.next != None and n1.next == None:
                n1.next = ListNode(0)
            n.next = ListNode(n1.next.val + n2.next.val + n.val // 10)
            n.val = n.val % 10
            n1, n2, n = n1.next, n2.next, n.next
        if n.val >= 10:
            n.val = n.val - 10
            n.next = ListNode(1)
        return l
        
if __name__ == '__main__':
    s = Solution()
    l1 = ListNode(2, ListNode(4, ListNode(3)))
    l2 = ListNode(5, ListNode(6, ListNode(4)))
    l = s.addTwoNumbers(l1, l2)
    while l:
        print(l.val)
        l = l.next
    
    l1 = ListNode(0)
    l2 = ListNode(0)
    l = s.addTwoNumbers(l1, l2)
    while l:
        print(l.val)
        l = l.next
    
    l1 = ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9))))))))))
    l2 = ListNode(9, ListNode(9, ListNode(9, ListNode(9))))
    l = s.addTwoNumbers(l1, l2)
    while l:
        print(l.val)
        l = l.next